// pages/main/hudong/SuggestionBox.js
import api from "../../../utils/api.js";
import utils from "../../../utils/utils.js";
import network from "../../../utils/network.js";
import formatDate from "../../../utils/format_date.js";
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    finish: true,
    filename: null,
    filePath: null,
    fileId: [],
    signPath: '',
    fileType: null,
    sysmodenum:'miniofficia',
    shownext:false,

    sysnextopt:'',
    sysnextoptid:'',
    setid:97,

    isHidden: true,//原始添加
    url: [],//上传文件路径
    type: '',//上传文件类型
    uploaderList: [],//上传文件数组
    showModal: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.signature = this.selectComponent("#signature");
    this.toast = this.selectComponent("#toast");


  },

  bindNextChange: function (e) {
    let applyIndex = e.detail.value
    let nextUserList = this.data.nextUsers
    let dataNextUser = this.data.dataNextUser
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      applyIndex:applyIndex,
      sysnextoptid:nextUserList[applyIndex].id,
      sysnextopt:dataNextUser[applyIndex].name
    })
  },

  upload() {
    let that = this;
    wx.chooseMessageFile({
      count: 1,
      type: 'all',
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFiles
        let url = app.globalData.debug ? api.debugUrl : api.url
        wx.uploadFile({
          url: url + api.upFile, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0].path,
          name: 'file',
          formData: {
            adminid: app.globalData.userInfo.uid,
            token: app.globalData.userInfo.token,
            cfrom: "weixin",
            device: app.globalData.device
          },
          success(res) {
            const data = res.data
            console.log(JSON.parse(res.data).id)
            that.data.fileId =JSON.parse(res.data).id
            that.setData({
              filename: JSON.parse(res.data).filename
            })
          }
        })
      }
    })
  },

    // // 上传图片
    upImg() {
      var that = this
  
      wx.chooseMessageFile({
        count: 1,
        type: 'all',
        success(res) {
          // tempFilePath可以作为img标签的src属性显示图片
          //  console.log('vvvvvvvv',res.tempFiles)
  
          const tempFilePaths = res.tempFiles[0].path
          const type = res.tempFiles[0].type
          let url = app.globalData.debug ? api.debugUrl : api.url
          wx.uploadFile({
            url: url + api.upFile,
            filePath: tempFilePaths,
            name: 'file',
            formData: {
              adminid: app.globalData.userInfo.uid,
              token: app.globalData.userInfo.token,
              cfrom: "weixin",
              device: app.globalData.device
            },
            success(res) {
              const datas = JSON.parse(res.data)
            
              that.data.list
             
              var upFiles = datas.data
              if (upFiles != '') {
                let tempFile = tempFilePaths;
                let uploaderList = that.data.uploaderList.concat(tempFile);
                that.data.url = that.data.url.concat(upFiles);
                let fileId =that.data.fileId.concat(datas.id)
                that.setData({
                  type: type,
                  uploaderList: uploaderList,
                  fileId:fileId
                })
        
              }
              
            }
          })
  
        }
      })
  
    },
  
    // 删除上传
    close(e) {
      var that = this
      var nowList = [];//新数据
      var uploaderList = that.data.uploaderList;//原数据
  
      for (let i = 0; i < uploaderList.length; i++) {
        if (i == e.currentTarget.dataset.index) {
          continue;
        } else {
          nowList.push(uploaderList[i])
        }
      }
      that.setData({
        uploaderList: nowList,
        isHidden: true
      })
    },

  chooseType() {
    let that = this;
    wx.showActionSheet({
      itemList: this.data.fileTypes,
      success(res) {
        that.setData({
          fileType: that.data.fileTypes[res.tapIndex]
        })
      }
    })

  },

  goSignature() {
    this.signature.openSignature();
  },

  getSignature(src) {
    this.setData({
      path: src.detail
    })
    let that = this;
    let url = app.globalData.debug ? api.debugUrl : api.url
    wx.uploadFile({
      url: url + api.upFile, //仅为示例，非真实的接口地址
      filePath: this.data.path,
      name: 'file',
      formData: {
        adminid: app.globalData.userInfo.uid,
        token: app.globalData.userInfo.token,
        cfrom: "weixin",
        device: app.globalData.device
      },
      success(res) {
        const data = res.data
        that.data.signPath =  JSON.parse(res.data).filepath
      }
    })
  },

  formSubmit: function(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    let formValue = e.detail.value;
    
    if (e.detail.value.title == '') {
      this.toast.showToast('请输入意见标题');
      return;
    } else if (e.detail.value.phone == '' || !utils.isTelNum(e.detail.value.phone)) {
      this.toast.showToast('请输入正确的联系方式');
      return;
    } else if(e.detail.value.details == ''){
      this.toast.showToast('请输入详细意见');
      return;
    }
    // else if (!this.data.signPath) {
    //   this.toast.showToast('请上传签名');
    //   return;
    // }



    
      var postjson = {

      title: e.detail.value.title,
      phone: e.detail.value.phone,
      details:e.detail.value.remarks
      }
    

    network.postJson(this, api.saveOpinion, postjson).then(res => {
      this.toast.showToast('提交成功');
    });
  }
})